package com.ssis.graphics;

import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;
//import org.newdawn.slick.util.Log;

public class Sprite {

	Texture texture;
	private int x;
	private int y;
	private int frame;
	//private int sheetrows;
	private int sheetcols;
	private int height;
	private int width;
	
	public Sprite(String texturename, int x, int y) {
		this(texturename, x, y, 0, 1, 1);
	}
	
	public Sprite(String texturename, int x, int y, int frame, int sheetrows, int sheetcols) {
		texture = Textures.getTexture(texturename);
		
		this.x = x;
		this.y = y;
		this.frame = frame;
		//this.sheetrows = sheetrows;
		this.sheetcols = sheetcols;
		this.height = texture.getTextureHeight()/sheetrows;
		this.width = texture.getTextureWidth()/sheetcols;
	}
	
	public void render() {
		int row = frame/sheetcols;
		int col = frame%sheetcols;
		float texturexmin = (float)(col * width) / texture.getTextureWidth();
		float texturexmax = (float)((col+1) * width) / texture.getTextureWidth();
		float textureymin = (float)(row * height) / texture.getTextureHeight();
		float textureymax = (float)((row+1) * height) / texture.getTextureHeight();

		//Log.debug("texture coords: " + texturexmin + " " + texturexmax + " " + textureymin + " " + textureymax);
		//Log.debug("texture mapped: " + x + " " + (x+width) + " " + y + " " + (y+height));
		
		texture.bind();
		GL11.glBegin(GL11.GL_QUADS);
		GL11.glTexCoord2f(texturexmin,textureymin);
		GL11.glVertex2f(x,y);
		GL11.glTexCoord2f(texturexmax,textureymin);
		GL11.glVertex2f(x+width,y);
		GL11.glTexCoord2f(texturexmax,textureymax);
		GL11.glVertex2f(x+width,y+height);
		GL11.glTexCoord2f(texturexmin,textureymax);
		GL11.glVertex2f(x,y+height);
		GL11.glEnd();
	}
	
	public void changeFrame(int frame) {
		this.frame = frame;
	}
	
	public void moveto(int x, int y) {
		this.x = x;
		this.y = y;
	}
	
	public void translate(int x, int y) {
		this.x += x;
		this.y += y;
	}
	
	public int getX() {
		return x;
	}
	
	public int getY() {
		return y;
	}
}
